<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>buying_courses</title>
<link type="text/css" rel="stylesheet" href="__CSS__/bootstrap.min.css">
<link type="text/css" rel="stylesheet" href="__CSS__/zhiye.css">
<script src="__JS__/jquery.min.js"></script>
<script src="__JS__/bootstrap.min.js"></script>

<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://cdn.bootcss.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
<body>
<div class="container"style="background-color: black">
    <div class="row"  style="padding: 1rem;">
        <div class="col-xs-12" >
            <a href="{:url('Index/index')}"  style="font-size: 3rem;color:white;">
                <span class="glyphicon glyphicon-tint"></span>在线学习
            </a>

        </div>
    </div>
</div>

    <div class="container">
        <div class="row text-center" style="margin: 5rem">
            <div style="margin-top: 1rem">
                <h4> 购买课程权限</h4>
            </div>
            <div style="margin-top: 3rem">
                <h3>{$course.record_name}</h3>
            </div>
            <div style="margin-top: 2rem">
                <h1>{$course.charge}</h1>
            </div>

            <div style="margin-top: 2rem">
                <input type="password" id="buying_courses" class="input" placeholder="密码" style="outline: none;border:none;border-bottom:1px grey solid">
            </div>
            <div  style="margin-top: 2rem" onclick="buying_courses()">
                <button class="btn btn-lg">确认购买</button>
            </div>
        </div>
    </div>
</body>
</html>
<script>
    var url="{:url('Chapter/moeny')}";//查余额url
    var goumai="{:url('Chapter/purchase')}";//查余额url
    var course_id="{$course.id}";
    var Recharge="{:url('Personal/Recharge')}";
    var chaptershow="{:url('Chapter/chaptershow')}";

    function buying_courses() {
        $.ajax({
            url:url,
            data:{
                course_id: course_id
            },
            dataType:"text",
            type:"post",
            success:function (data) {
                if(data=="true"){
                            zhifu(course_id)
                }else{
                    if(confirm("余额不足是否前往充值？")){
                            window.location.href=Recharge
                    }else{
                        history.go(-1)
                    }
                }
            }
        })
    }
    function zhifu(course_id) {
        $.ajax({
            url:goumai,
            data:{
                password: $("#buying_courses").val(),
                course_id: course_id
            },
            dataType:"json",
            type:"post",
            success:function (data) {
//                alert(data);
             switch (data.content){
                 case "repeat"  :
                     alert(" 你已经买过了");
                     break;
                 case "password_no"  :
                     alert("密码错误")
                     break;
                 case "balance_true"  :
                     alert(" 购买成功");
                    window.location.href=chaptershow+"?id="+course_id
                     break;
                 case "balance_false"  :
                     alert(" 购买失败")
                     break;

             }
//                if(data=="true"){
//                    alert("购买成功")
//                    history.go(-1)
//                }else{
//                   alert("购买失败")
//                    history.go(-1)
//                }
            }
        })
    }
</script>